/*
 * can_driver.c
 *
 *  Created on: 2023��4��11��
 *      Author: chenwangang
 */


#include "bsp_can_driver.h"
#include "bsp_can_cfg.h"
#include "user_define.h"
#include "timers.h"
#include <string.h>


#if(CAN_USED == TRUE)

/*-----------------------------------------------------------*/
volatile uint8_t g_ucIsCountTime = 0u;

can_message_t canRecvMsg;
can_message_t canRecvMsg2;
can_message_t canRecvMsg3;
can_message_t canRecvMsg4;
can_message_t canSendMsg = {
    .cs     = 0,
    .id     = DOMAIN_BEHIND,
    .length = 8,
};;


static void _can_rx_filiter(void);
static void _config_rx_buffer(void);
static void _config_tx_buffer(void);


extern union protocol_SendMsg g_send_msg;

extern QueueHandle_t g_xQueue_Rx_Can_Msg1;
extern QueueHandle_t g_xQueue_Rx_Can_Msg2;
extern QueueHandle_t g_xQueue_Rx_Can_Msg3;
extern QueueHandle_t g_xQueue_Rx_Can_Msg4;

/*-----------------------------------------------------------*/
/* Config CAN filiter is individual */
static void _can_rx_filiter(void)
{
    uint32_t index = 0;

    for (index = 0u; index < g_rx_can_msg_id_num; index++)
    {
        CAN_SetRxFilter(&can_pal1_instance, g_rx_msg_config[index].RxID_Type, 
        g_rx_msg_config[index].usRxMailBox, g_rx_msg_config[index].usRxMask);
    }
}

/* Config rx MB buffer */
static void _config_rx_buffer(void)
{
    uint8_t index = 0u;

    /* Configure RX buffer with index RX_MAILBOX for Function ID and Physical ID*/
    for (index = 0u; index < g_rx_can_msg_id_num; index++)
    {
        /* According to Rx msg ID type to configure MB msg ID type */
        buff_RxTx_Cfg.idType = g_rx_msg_config[index].RxID_Type;

        CAN_ConfigRxBuff(&can_pal1_instance, g_rx_msg_config[index].usRxMailBox, 
        &buff_RxTx_Cfg, g_rx_msg_config[index].usRxID);
    }
}

/* Config tx MB buffer */
static void _config_tx_buffer(void)
{
    uint8_t index = 0u;

    /* Configure TX buffer with index RX_MAILBOX for Function ID and Physical ID*/
    for (index = 0u; index < g_tx_can_msg_id_num; index++)
    {
        /* According to Tx msg ID type to config MB msg ID type */
        buff_RxTx_Cfg.idType = g_tx_msg_config[index].TxID_Type;

        /* Config Tx buffer with index Tx mailbox */
        CAN_ConfigTxBuff(&can_pal1_instance, g_tx_msg_config[index].ucTxMailBox, &buff_RxTx_Cfg);
    }
}

/*CAN0回调函数*/
void CAN0_Callback_Func(uint32_t instance,can_event_t event,uint32_t buffIdx,void *flexcanState)
{
    (void)flexcanState; //此处防止警报
    (void)instance;
    (void)buffIdx;
    BaseType_t ret = pdFALSE;

    switch(buffIdx)
    {
        case RX_MAILBOX_1_NORMAL:
            CAN_Receive(&can_pal1_instance, RX_MAILBOX_1_NORMAL, &g_gate_can_recv_msg);
            ret = xQueueSendFromISR(g_xQueue_Rx_Can_Msg1, &g_gate_can_recv_msg, queueSendDONT_BLOCK);
            break;
        case RX_MAILBOX_2_NORMAL:
            CAN_Receive(&can_pal1_instance, RX_MAILBOX_2_NORMAL, &g_htcu_can_recv_msg);
            ret = xQueueSendFromISR(g_xQueue_Rx_Can_Msg2, &g_htcu_can_recv_msg, queueSendDONT_BLOCK);
            break;
        case RX_MAILBOX_3_NORMAL:
            CAN_Receive(&can_pal1_instance, RX_MAILBOX_3_NORMAL, &g_sensor_can_recv_msg);
            ret = xQueueSendFromISR(g_xQueue_Rx_Can_Msg3, &g_sensor_can_recv_msg, queueSendDONT_BLOCK);
            break;
        case RX_MAILBOX_4_NORMAL:
            CAN_Receive(&can_pal1_instance, RX_MAILBOX_4_NORMAL, &g_musicloud_can_recv_msg);
            ret = xQueueSendFromISR(g_xQueue_Rx_Can_Msg4, &g_musicloud_can_recv_msg, queueSendDONT_BLOCK);
            break;

        default:
            break;
    }
}



/* Init CAN moudule */
void Bsp_CAN_Init(void)
{
    uint8_t index = 0;

    

    CAN_Init(&can_pal1_instance, &can_pal1_Config0);

    /*注册接收配置和RX_MSG_ID过滤*/
    _config_rx_buffer();

    /*配置发送*/
    _config_tx_buffer();

    /*设置RX_MSG_ID掩码*/
    _can_rx_filiter();

    /*注册回调函数*/
    CAN_InstallEventCallback(&can_pal1_instance, &CAN0_Callback_Func, (isr_t *)0);

    /*接收报文并重新注册回调函数*/
    // for (index = 0u; index < g_rx_can_msg_id_num; index++)
    // {
    //     CAN_Receive(&can_pal1_instance, g_rx_msg_config[index].usRxMailBox, &g_can_recv_msg);
    // }
    CAN_Receive(&can_pal1_instance, RX_MAILBOX_1_NORMAL, &g_gate_can_recv_msg);
    CAN_Receive(&can_pal1_instance, RX_MAILBOX_2_NORMAL, &g_htcu_can_recv_msg);
    CAN_Receive(&can_pal1_instance, RX_MAILBOX_3_NORMAL, &g_sensor_can_recv_msg);
    CAN_Receive(&can_pal1_instance, RX_MAILBOX_4_NORMAL, &g_musicloud_can_recv_msg);

    /* set priortiy of can interrupt*/
    INT_SYS_SetPriority(CAN0_ORed_IRQn, (configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY + 1));
    INT_SYS_SetPriority(CAN0_ORed_0_15_MB_IRQn, (configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY + 2));
}


/*transmitted can message*/
static uint8_t gs_ucIsTransmittedMsg = FALSE;
/*transmitted can message callback*/
void transmit_can_msg_callback(void)
{
    gs_ucIsTransmittedMsg = TRUE;
}

#endif